home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJEMU106.ARJ / COMPARE.CC next >
C/C++ Source or Header  |  1992-04-08  |  2KB  |  90 lines

  1. #include "emu.h"
  2. #include "compare.h"
  3. #include "const.h"
  4.  
  5. #define NAN_NONE 0
  6. #define NAN_SNAN 1
  7. #define NAN_QNAN 2
  8.  
  9. extern "C" void djshld(void *);
  10.  
  11. int nan_type(reg& r)
  12. {
  13.   if (r.exp != EXP_MAX)
  14.     return NAN_NONE;
  15.   if (r.sigh & 0x40000000)
  16.     return NAN_QNAN;
  17.   return NAN_SNAN;
  18. }
  19.  
  20. int compare(reg& a, reg& b)
  21. {
  22.   int a_inf, b_inf; // 0=no, 1=pos, -1=neg
  23.   a_inf = 0;
  24.   if (val_same(a, CONST_PINF))
  25.     a_inf = 1;
  26.   else if (val_same(a, CONST_NINF))
  27.     a_inf = -1;
  28.   b_inf = 0;
  29.   if (val_same(b, CONST_PINF))
  30.     b_inf = 1;
  31.   else if (val_same(b, CONST_NINF))
  32.     b_inf = -1;
  33.   if (a_inf || b_inf)
  34.   {
  35.     if (a_inf == 1)
  36.       if (b_inf == 1)
  37.         return COMP_A_EQ_B;
  38.       else
  39.         return COMP_A_GT_B;
  40.     if (b_inf == 1)
  41.       return COMP_A_LT_B;
  42.  
  43.     if (a_inf == -1)
  44.       if (b_inf == -1)
  45.         return COMP_A_EQ_B;
  46.       else
  47.         return COMP_A_LT_B;
  48.     if (b_inf == -1)
  49.       return COMP_A_GT_B;
  50.   }
  51.   int a_nan = nan_type(a);
  52.   int b_nan = nan_type(b);
  53.   if (a_nan || b_nan)
  54.   {
  55.     if ((a_nan == NAN_SNAN) || (b_nan == NAN_SNAN))
  56.       return COMP_NOCOMP | COMP_SNAN | COMP_NAN;
  57.     return COMP_NOCOMP | COMP_NAN;
  58.   }
  59.   if (a.sign != b.sign)
  60.   {
  61.     if ((a.tag == TW_Z) && (b.tag == TW_Z))
  62.       return COMP_A_EQ_B;
  63.     if (a.sign == SIGN_POS)
  64.       return COMP_A_GT_B;
  65.     return COMP_A_LT_B;
  66.   }
  67.   while (!(a.sigh & 0x80000000))
  68.   {
  69.     if (!a.exp)
  70.       break;
  71.     djshld(&a.sigl);
  72.     a.exp--;
  73.   }
  74.   while (!(b.sigh & 0x80000000))
  75.   {
  76.     if (!b.exp)
  77.       break;
  78.     djshld(&b.sigl);
  79.     b.exp--;
  80.   }
  81.   int diff = a.exp - b.exp;
  82.   if (diff == 0) diff = a.sigh - b.sigh;
  83.   if (diff == 0) diff = a.sigl - b.sigl;
  84.   if (a.sign == SIGN_NEG)
  85.     diff = -diff;
  86.   if (diff > 0) return COMP_A_GT_B;
  87.   if (diff < 0) return COMP_A_LT_B;
  88.   return COMP_A_EQ_B;
  89. }
  90.